import Immutable from 'immutable';

const defaultState = new Immutable.List();

export default function MessageHandlerReducer(state = defaultState, action) {
  switch(action.type) {
    case 'LOAD_MESSAGE_HANDLER':
      var data = action.data;
      if (data.success) {
        return data.data;
      }else {
        return [];
      }
      break;
    case 'CREATE_MESSAGE_HANDLER':
      var data = action.data;
      console.log(action)
      if (data.success) {
        return state.concat(data.data);
      }else {
        return state;
      }
      break;
    case 'DELETE_MESSAGE_HANDLER':
      console.log(action)
      state = _.remove(state, function(data) {
        console.log(data)
        return action.id.id !== data.id
      })
      return state;
    case 'EDIT_MESSAGE_HANDLER':
      var data = action.data
      console.log(data);
      return state;
      break;
    case 'ERROR_MESSAGE_HANDLER':
      return action.error.status;
    default:
      return state;
  }
}
